package com.yj.springbootaop.aop;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yj.springbootaop.anno.Log;
import com.yj.springbootaop.dao.SysLogDaoImpl;
import com.yj.springbootaop.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Slf4j
@Component
public class LogAop {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    SysLogDaoImpl sysLogDao;


    @Pointcut(value = "@annotation(com.yj.springbootaop.anno.Log)")
    public void pointCut() {

    }

    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Exception {
        Object result = null;
        Long beginTime = System.currentTimeMillis();
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            log.error("error around():{}", throwable.getMessage());
        }
        Long endTime = System.currentTimeMillis();

        Long time = endTime - beginTime;

        saveSysLog(joinPoint, time);

        return result;
    }

    private void saveSysLog(ProceedingJoinPoint joinPoint, Long time) throws Exception {
        // 获取切入点的签名：整个方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println(methodSignature);
        // 获取方法
        Method method = methodSignature.getMethod();

        // 获取方法上的注解
        Log log = method.getAnnotation(Log.class);
        String operation = "";
        if (log!=null){
            operation = log.value();
        }

        // 获取方法名
        String methodName = method.getName();

        // 获取方法上的参数
        Object[] args = joinPoint.getArgs();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        String requestURL = request.getRequestURL().toString();
//        final String ip = request.getLocalAddr();
        SysLog sysLog = new SysLog();
        sysLog.setOperation(operation);
        sysLog.setCreateTime(new Date());
        sysLog.setMethod(methodName);
        sysLog.setParams(objectMapper.writeValueAsString(args));
        sysLog.setUsername("yijian");
        sysLog.setIp(requestURL);
        sysLog.setTime(time.intValue());
        sysLogDao.save(sysLog);
    }

}
